﻿using FastExecutor.Base.Design;
using FastExecutor.Base.Model;
using FastExecutor.ORG.DB;
using FastExecutor.ORG.Entity;
using FastIOC.Annotation;
using FastORM.Applicaiton;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace FastExecutor.ORG.Controller
{
    [RouteArea("Frame")]
    [RoutePrefix("Role")]
    public class RoleController : FastTemplateController<Frame_Role>
    {
        [AutoWired]
        public DB_UserRoleRelation @DB_UserRoleRelation;
        [Route("List")]
        public ActionResult List()
        {
            return View("~/Views/Frame/Role/Role_List.cshtml");
        }
        [Route("Add")]
        public ActionResult Add()
        {
            return View("~/Views/Frame/Role/Role_Add.cshtml");
        }
        [Route("Edit")]
        public ActionResult Edit()
        {
            return View("~/Views/Frame/Role/Role_Edit.cshtml");
        }
        [Route("RelationList")]
        public ActionResult RelationList()
        {
            return View("~/Views/Frame/Role/UserRole_List.cshtml");
        }
        [Route("RelationEdit")]
        public ActionResult RelationEdit()
        {
            return View("~/Views/Frame/Role/UserRole_Edit.cshtml");
        }

        [HttpPost, Route("FindList")]
        public override ActionResult FindList()
        {
            JObject Params = GetParams();
            string RoleName = Convert.ToString(Params["RoleName"]);
            string GroupGuid = Convert.ToString(Params["GroupGuid"]);
            if (!string.IsNullOrEmpty(RoleName))
            {
                ListWhereText += string.Format(" and RoleName like '%{0}%'", RoleName);
            }
            if (!string.IsNullOrEmpty(GroupGuid))
            {
                ListWhereText += string.Format(" and GroupGuid='{0}'", GroupGuid);
            }
            return base.FindList();
        }

        [HttpPost, Route("FindDetail")]
        public override ActionResult FindDetail()
        {
            return base.FindDetail();
        }

        [HttpPost, Route("OperateAdd")]
        public override ActionResult OperateAdd(Frame_Role Entity)
        {
            ReturnValue result = new ReturnValue();
            DBRow row = new DBRow("Frame_Role", "RoleName", Entity.RoleName);
            if (row.IsExist())
            {
                result.Add("code", -100);
                result.Add("msg", "已存在相同名称的角色！");
                return result;
            }
            else
            {
                return base.OperateAdd(Entity);
            }

        }

        [HttpPost, Route("OperateUpdate")]
        public override ActionResult OperateUpdate()
        {
            return base.OperateUpdate();
        }


        [HttpPost, Route("OperateUpdateRelation")]
        public ActionResult OperateUpdateRelation()
        {
            ReturnValue result = new ReturnValue();
            JObject Params = GetParams();
            String UserGuid = Convert.ToString(Params.GetValue("UserGuid"));
            String RoleList = Convert.ToString(Params.GetValue("RoleList"));
            string sql = string.Format("delete from frame_userrolerelation where UserGuid='{0}'", UserGuid);
            @DBUtil.ExecuteSQL(sql);
            List<Frame_Role> RoleEntityList = JsonConvert.DeserializeObject<List<Frame_Role>>(RoleList);
            foreach (Frame_Role Role in RoleEntityList)
            {
                if (Role.IsCheck)
                {
                    Frame_UserRolerelation Relation = new Frame_UserRolerelation();
                    Relation.UserGuid = UserGuid;
                    Relation.RoleGuid = Role.RowGuid;
                    @DB_UserRoleRelation.Insert(Relation);
                }
            }
            return result;
        }

        [HttpPost, Route("OperateBatchSaveRelation")]
        public ActionResult OperateBatchSaveRelation()
        {
            JObject Params = GetParams();
            ReturnValue result = new ReturnValue();
            string UserList = Convert.ToString(Params.GetValue("UserList"));
            JArray UserArray = JsonConvert.DeserializeObject<JArray>(UserList);
            foreach (JObject User in UserArray)
            {
                string UserGuid = Convert.ToString(User.GetValue("RowGuid"));
                string RoleList = Convert.ToString(User.GetValue("RoleList"));
                string sql = string.Format("delete from frame_userrolerelation where UserGuid='{0}'", UserGuid);
                @DBUtil.ExecuteSQL(sql);
                List<Frame_Role> RoleEntityList = JsonConvert.DeserializeObject<List<Frame_Role>>(RoleList);
                foreach (Frame_Role Role in RoleEntityList)
                {
                    if (Role.IsCheck)
                    {
                        Frame_UserRolerelation Relation = new Frame_UserRolerelation();
                        Relation.UserGuid = UserGuid;
                        Relation.RoleGuid = Role.RowGuid;
                        @DB_UserRoleRelation.Insert(Relation);
                    }
                }
            }
            return result;
        }

        [HttpPost, Route("OperateBatchDelete")]
        public override ActionResult OperateBatchDelete()
        {
            return base.OperateBatchDelete();
        }
    }
}
